package cn.intotw.rdcj.admin.shiro;

import cn.intotw.rdcj.admin.domain.dao.RolesResourceDao;
import cn.intotw.rdcj.admin.domain.dao.RolesUserDao;
import cn.intotw.rdcj.admin.domain.dao.UserBaseDao;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.redisson.api.RedissonClient;
import org.redisson.cache.CacheKey;
import org.redisson.spring.cache.CacheConfig;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by Chenxiang
 *
 * @generator: IntelliJ IDEA
 * @description: shiro配置bean
 * @project: rdcj
 * @package: cn.intotw.rdcj.admin.config
 * @date: 2020年12月22日 10时38分
 */
@Configuration
public class ShiroConfiguration {
    @Bean
    CacheManager shiroCacheManager(RedissonClient redissonClient){
        final ShiroRedissonSpringCacheManager redissonSpringCacheManager = new ShiroRedissonSpringCacheManager(redissonClient);
        return new ShiroCacheManager(redissonSpringCacheManager);
    }
    @Bean
    public RdcjAdminRealm rdcjAdminRealm(CacheManager cacheManager, RolesUserDao rolesUserDao, UserBaseDao userBaseDao, RolesResourceDao rolesResourceDao){
        return new RdcjAdminRealm(cacheManager,rolesUserDao,userBaseDao,rolesResourceDao);
    }
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(RdcjAdminRealm rdcjAdminRealm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(rdcjAdminRealm);
        return defaultWebSecurityManager;
    }
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        //设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        //设置未认证(登录)时，访问需要认证的资源时跳转的页面
        shiroFilterFactoryBean.setLoginUrl("/html/login.html");

        //设置访问无权限的资源时跳转的页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauthorizedPage");
        Map<String, Filter> filterMap = new HashMap<>();
        filterMap.put("tryLogin",new AnonAndTryLoginFilter());
        shiroFilterFactoryBean.setFilters(filterMap);

        //指定路径和过滤器的对应关系
        Map<String, String> filterChainMap = new HashMap<>();
        //设置/user/login不需要登录就能访问
        filterChainMap.put("/css/**", "anon");
        filterChainMap.put("/font-awesome/**", "anon");
        filterChainMap.put("/fonts/**", "anon");
        filterChainMap.put("/img/**", "anon");
        filterChainMap.put("/js/**", "anon");
        filterChainMap.put("/locales/**", "anon");
        filterChainMap.put("/html/login.html", "anon");
        filterChainMap.put("/favicon.ico", "anon");
        filterChainMap.put("/admin/login", "anon");
        filterChainMap.put("/**", "tryLogin");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainMap);
        return shiroFilterFactoryBean;
    }
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager defaultWebSecurityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(defaultWebSecurityManager);
        return authorizationAttributeSourceAdvisor;
    }
}
